/* Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You under the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "zobjects_gtk.h"

void OnGtkRadioButtonToggleEventProc( GtkWidget *widget , gpointer data )
{
	struct WindowCallbackControl			*win_callback_ctl = (struct WindowCallbackControl *)data ;
	void						*guito_direct_prop = GetObjectDirectProperty(win_callback_ctl->guictl_obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guito_common = (struct ZlangDirectProperty_guiwidget_common *)guito_direct_prop ;
	unsigned char					is_checked ;
	struct StringListNode				*c2c = NULL ;
	struct StringListNode				*next_c2c = NULL ;
	struct WindowCallbackControl			*set_wcc = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*set_common = NULL ;
	
	is_checked = gtk_check_button_get_active( GTK_CHECK_BUTTON(guito_common->gtk_widget) ) ;

	list_for_each_entry_safe( c2c , next_c2c , & (guito_common->ctl_set_enable_to_ctl_list) , struct StringListNode , str_list_node )
	{
		set_wcc = GetControlByName( guito_common->guiwindow_obj , c2c->str ) ;
		if( set_wcc )
		{
			set_common = (struct ZlangDirectProperty_guiwidget_common *)GetObjectDirectProperty( set_wcc->guictl_obj ) ;
			gtk_widget_set_sensitive( set_common->gtk_widget , is_checked ) ;
		}
	}

	return;
}

ZlangInvokeFunction ZlangInvokeFunction_guiradiobutton_Create_guiwindow_string_string;
int ZlangInvokeFunction_guiradiobutton_Create_guiwindow_string_string( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*in2 = GetInputParameterInLocalObjectStack(rt,2) ;
	struct ZlangObject				*in3 = GetInputParameterInLocalObjectStack(rt,3) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	struct ZlangDirectProperty_guiradiobutton	*guiradiobutton_direct_prop = GetObjectDirectProperty(out1) ;
	struct ZlangDirectProperty_guiwidget_common	*guiradiobutton_common = & (guiradiobutton_direct_prop->common) ;
	char						*group_name = NULL ;
	char						*str = NULL ;
	struct WindowControlGroup			*gtk_ctl_grp = NULL ;

	CallRuntimeFunction_string_GetStringValue( rt , in2 , & group_name , NULL );
	CallRuntimeFunction_string_GetStringValue( rt , in3 , & str , NULL );

	gtk_ctl_grp = GetGtkControlGroup( rt , in1 , group_name ) ;
	if( gtk_ctl_grp == NULL )
	{
		gtk_ctl_grp = AddGtkControlGroup( rt , in1 , group_name ) ;
		if( gtk_ctl_grp == NULL )
		{
			UnreferObject( rt , out1 );
			return GetRuntimeErrorNo(rt);
		}

		EVAL_GTK_WIDGET_NEW_IN_CREATE( guiradiobutton_common->gtk_widget , gtk_check_button_new_with_label(str) )
		SET_CONTROL_DEFAULT_STYLE( in1 , guiradiobutton_common->gtk_widget )

		ADD_GTK_WIDGET_TO_GSLIST( gtk_ctl_grp , guiradiobutton_direct_prop )

		gtk_ctl_grp->first_gtk_widget = guiradiobutton_common->gtk_widget ;
	}
	else
	{
		EVAL_GTK_WIDGET_NEW_IN_CREATE( guiradiobutton_common->gtk_widget , gtk_check_button_new_with_label(str) )
		SET_CONTROL_DEFAULT_STYLE( in1 , guiradiobutton_common->gtk_widget )

		ADD_GTK_WIDGET_TO_GSLIST( gtk_ctl_grp , guiradiobutton_direct_prop )

		gtk_check_button_set_group( GTK_CHECK_BUTTON(guiradiobutton_common->gtk_widget) , GTK_CHECK_BUTTON(gtk_ctl_grp->first_gtk_widget) );
	}

	TRANSFER_INPUT_OBJECT_TO_NEW_OBJECT( guiradiobutton_common->guiwindow_obj , in1 )

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiradiobutton_SetChecked_bool;
int ZlangInvokeFunction_guiradiobutton_SetChecked_bool( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guieditarea		*guiradiobutton_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guiradiobutton_common = (struct ZlangDirectProperty_guiwidget_common *)guiradiobutton_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	unsigned char					is_checked ;

	CallRuntimeFunction_bool_GetBoolValue( rt , in1 , & is_checked );

	gtk_check_button_set_active( GTK_CHECK_BUTTON(guiradiobutton_common->gtk_widget) , is_checked );

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZlangInvokeFunction ZlangInvokeFunction_guiradiobutton_SetToggledEventCallback_functionptr;
int ZlangInvokeFunction_guiradiobutton_SetToggledEventCallback_functionptr( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guibutton		*guiradiobutton_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guiradiobutton_common = (struct ZlangDirectProperty_guiwidget_common *)guiradiobutton_direct_prop ;
	struct ZlangObject				*in1 = GetInputParameterInLocalObjectStack(rt,1) ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;

	ADD_CONTROL_GENERAL_CALLBACK_IN_CREATE( guiradiobutton_common->guiwindow_obj , obj , GTK_EVENT_TOGGLED , in1 )

	TRANSFER_MASTER_OBJECT_TO_OUTPUT_OBJECT

	return 0;
}

ZLANG_INVOKE_FUNCTION_guiwidget_all_common_functions( radiobutton )

ZLANG_INVOKE_FUNCTION_guictl_all_common_functions( radiobutton )

ZlangInvokeFunction ZlangInvokeFunction_guiradiobutton_IsChecked;
int ZlangInvokeFunction_guiradiobutton_IsChecked( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guieditarea		*guiradiobutton_direct_prop = GetObjectDirectProperty(obj) ;
	struct ZlangDirectProperty_guiwidget_common	*guiradiobutton_common = (struct ZlangDirectProperty_guiwidget_common *)guiradiobutton_direct_prop ;
	struct ZlangObject				*out1 = GetOutputParameterInLocalObjectStack(rt,1) ;
	unsigned char					is_checked ;

	is_checked = gtk_check_button_get_active( GTK_CHECK_BUTTON(guiradiobutton_common->gtk_widget) ) ;

	CallRuntimeFunction_bool_SetBoolValue( rt , out1 , is_checked );

	return 0;
}

ZlangCreateDirectPropertyFunction ZlangCreateDirectProperty_guiradiobutton;
void *ZlangCreateDirectProperty_guiradiobutton( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiradiobutton	*guiradiobutton_prop = NULL ;
	struct ZlangDirectProperty_guiwidget_common	*guiradiobutton_common = NULL ;
	
	guiradiobutton_prop = (struct ZlangDirectProperty_guiradiobutton *)ZLMALLOC( sizeof(struct ZlangDirectProperty_guiradiobutton) ) ;
	if( guiradiobutton_prop == NULL )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_ALLOC , "alloc memory for entity" )
		return NULL;
	}
	memset( guiradiobutton_prop , 0x00 , sizeof(struct ZlangDirectProperty_guiradiobutton) );

	guiradiobutton_common = (struct ZlangDirectProperty_guiwidget_common *)guiradiobutton_prop ;
	guiradiobutton_common->gtk_widget_type_id = GTK_TYPE_CHECK_BUTTON ;

	INIT_ZLANGDIRECTPROPERTY_guiwidget_common(guiradiobutton)
	
	return guiradiobutton_prop;
}

ZlangDestroyDirectPropertyFunction ZlangDestroyDirectProperty_guiradiobutton;
void ZlangDestroyDirectProperty_guiradiobutton( struct ZlangRuntime *rt , struct ZlangObject *obj )
{
	struct ZlangDirectProperty_guiradiobutton	*guiradiobutton_direct_prop = GetObjectDirectProperty(obj) ;
	
	CLEAN_ZLANGDIRECTPROPERTY_guiwidget_common(guiradiobutton)

	ZLFREE( guiradiobutton_direct_prop );
	
	return;
}

ZlangSummarizeDirectPropertySizeFunction ZlangSummarizeDirectPropertySize_guiradiobutton;
void ZlangSummarizeDirectPropertySize_guiradiobutton( struct ZlangRuntime *rt , struct ZlangObject *obj , size_t *summarized_obj_size , size_t *summarized_direct_prop_size )
{
	SUMMARIZE_SIZE( summarized_direct_prop_size , sizeof(struct ZlangDirectProperty_guiradiobutton) )
	return;
}

static struct ZlangDirectFunctions direct_funcs_guiradiobutton =
	{
		ZLANG_OBJECT_guiradiobutton , /* char *ancestor_name */
		
		ZlangCreateDirectProperty_guiradiobutton , /* ZlangCreateDirectPropertyFunction *create_entity_func */
		ZlangDestroyDirectProperty_guiradiobutton , /* ZlangDestroyDirectPropertyFunction *destroy_entity_func */
		
		NULL , /* ZlangFromCharPtrFunction *from_char_ptr_func */
		NULL , /* ZlangToStringFunction *to_string_func */
		NULL , /* ZlangFromDataPtrFunction *from_data_ptr_func */
		NULL , /* ZlangGetDataPtrFunction *get_data_ptr_func */
		
		NULL , /* ZlangOperatorFunction *oper_PLUS_func */
		NULL , /* ZlangOperatorFunction *oper_MINUS_func */
		NULL , /* ZlangOperatorFunction *oper_MUL_func */
		NULL , /* ZlangOperatorFunction *oper_DIV_func */
		NULL , /* ZlangOperatorFunction *oper_MOD_func */
		
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NEGATIVE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_NOT_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_BIT_REVERSE_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_PLUS_PLUS_func */
		NULL , /* ZlangUnaryOperatorFunction *unaryoper_MINUS_MINUS_func */
		
		NULL , /* ZlangCompareFunction *comp_EGUAL_func */
		NULL , /* ZlangCompareFunction *comp_NOTEGUAL_func */
		NULL , /* ZlangCompareFunction *comp_LT_func */
		NULL , /* ZlangCompareFunction *comp_LE_func */
		NULL , /* ZlangCompareFunction *comp_GT_func */
		NULL , /* ZlangCompareFunction *comp_GE_func */
		
		NULL , /* ZlangLogicFunction *logic_AND_func */
		NULL , /* ZlangLogicFunction *logic_OR_func */
		
		NULL , /* ZlangLogicFunction *bit_AND_func */
		NULL , /* ZlangLogicFunction *bit_XOR_func */
		NULL , /* ZlangLogicFunction *bit_OR_func */
		NULL , /* ZlangLogicFunction *bit_MOVELEFT_func */
		NULL , /* ZlangLogicFunction *bit_MOVERIGHT_func */
		
		ZlangSummarizeDirectPropertySize_guiradiobutton , /* ZlangSummarizeDirectPropertySizeFunction *summarize_direct_prop_size_func */
	} ;

ZlangImportObjectFunction ZlangImportObject_guiradiobutton;
struct ZlangObject *ZlangImportObject_guiradiobutton( struct ZlangRuntime *rt )
{
	struct ZlangObject	*obj = NULL ;
	struct ZlangFunction	*func = NULL ;
	int			nret = 0 ;
	
	nret = ImportObject( rt , & obj , ZLANG_OBJECT_guiradiobutton , & direct_funcs_guiradiobutton , sizeof(struct ZlangDirectFunctions) , NULL ) ;
	if( nret )
	{
		SET_RUNTIME_ERROR( rt , RUNTIME_ERROR , ZLANG_ERROR_LINK_FUNC_TO_ENTITY , "import object to global objects heap" )
		return NULL;
	}
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiradiobutton_Create_guiwindow_string_string , ZLANG_OBJECT_guiradiobutton , "Create" , ZLANG_OBJECT_guiwindow,NULL , ZLANG_OBJECT_string,NULL , ZLANG_OBJECT_string,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiradiobutton_SetChecked_bool , ZLANG_OBJECT_guiradiobutton , "SetChecked" , ZLANG_OBJECT_bool,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiradiobutton_SetToggledEventCallback_functionptr , ZLANG_OBJECT_guiradiobutton , "SetToggledEventCallback" , ZLANG_OBJECT_functionptr,NULL , NULL ) ;
	if( func == NULL )
		return NULL;
	
	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guiwidget_all_common_functions( radiobutton )

	ADD_FUNCTION_AND_PARAMETERS_IN_OBJECT_guictl_all_common_functions( radiobutton )

	func = AddFunctionAndParametersInObject( rt , obj , ZlangInvokeFunction_guiradiobutton_IsChecked , ZLANG_OBJECT_bool , "IsChecked" , NULL ) ;
	if( func == NULL )
		return NULL;
	
	return obj ;
}

